<template>
   <div class="app-container">
      <div class="head-container">
         <a-form :model="queryParams" ref="queryRef" layout="inline" laba-width="68px">
            <a-form-item prop="title">
               <a-input v-model="queryParams.title" placeholder="请输入系统模块" clearable />
            </a-form-item>
            <a-form-item prop="operName">
               <a-input v-model="queryParams.operName" placeholder="请输入操作人员" clearable />
            </a-form-item>
            <a-form-item prop="businessType">
               <a-select v-model="queryParams.businessType" placeholder="操作类型" clearable>
                  <a-option v-for="dict in sys_oper_type" :key="dict.value" :label="dict.label" :value="dict.value" />
               </a-select>
            </a-form-item>
            <a-form-item prop="status">
               <a-select v-model="queryParams.status" placeholder="操作状态" clearable>
                  <a-option v-for="dict in sys_common_status" :key="dict.value" :label="dict.label"
                     :value="dict.value" />
               </a-select>
            </a-form-item>
            <a-form-item>
               <a-space>
                  <a-button type="primary" icon="Search" @click="handleQuery">
                     <template #icon>
                        <icon-search />
                     </template>
                     搜索</a-button>
                  <a-button icon="Refresh" @click="resetQuery">
                     <template #icon>
                        <icon-refresh />
                     </template>
                     重置</a-button>
               </a-space>
            </a-form-item>
         </a-form>
      </div>
      <div class="main-container">
         <div class="main-toolbar">
            <a-space>
               <a-button status="danger" size="small" :disabled="multiple" @click="handleDelete">
                  <template #icon>
                     <icon-delete />
                  </template>
                  删除</a-button>
               <a-button status="danger" size="small" @click="handleClean">
                  <template #icon>
                     <icon-delete />
                  </template>
                  清空</a-button>
            </a-space>
         </div>
         <div class="main-content">
            <a-table ref="operlogRef" :columns="columns" :data="operlogList">

            </a-table>
         </div>
      </div>

      <!-- 操作日志详细 -->
      <a-modal title="操作日志详细" v-model:visible="open" width="800px" append-to-body>
         <a-form :model="form" laba-width="100px">
            <a-row>
               <a-col :span="12">
                  <a-form-item label="操作模块：">{{ form.title }} / {{ typeFormat(form) }}</a-form-item>
                  <a-form-item label="登录信息：">{{ form.operName }} / {{ form.operIp }} / {{ form.operLocation
                     }}</a-form-item>
               </a-col>
               <a-col :span="12">
                  <a-form-item label="请求地址：">{{ form.operUrl }}</a-form-item>
                  <a-form-item label="请求方式：">{{ form.requestMethod }}</a-form-item>
               </a-col>
               <a-col :span="24">
                  <a-form-item label="操作方法：">{{ form.method }}</a-form-item>
               </a-col>
               <a-col :span="24">
                  <a-form-item label="请求参数：">{{ form.operParam }}</a-form-item>
               </a-col>
               <a-col :span="24">
                  <a-form-item label="返回参数：">{{ form.jsonResult }}</a-form-item>
               </a-col>
               <a-col :span="8">
                  <a-form-item label="操作状态：">
                     <div v-if="form.status === 0">正常</div>
                     <div v-else-if="form.status === 1">失败</div>
                  </a-form-item>
               </a-col>
               <a-col :span="8">
                  <a-form-item label="消耗时间：">{{ form.costTime }}毫秒</a-form-item>
               </a-col>
               <a-col :span="8">
                  <a-form-item label="操作时间：">{{ parseTime(form.operTime) }}</a-form-item>
               </a-col>
               <a-col :span="24">
                  <a-form-item label="异常信息：" v-if="form.status === 1">{{ form.errorMsg }}</a-form-item>
               </a-col>
            </a-row>
         </a-form>
      </a-modal>
   </div>
</template>

<script setup name="Operlog">
import { cleanOperlog, delOperlog, list } from "@/api/monitor/operlog";

const { proxy } = getCurrentInstance();
const { sys_oper_type, sys_common_status } = proxy.useDict("sys_oper_type", "sys_common_status");

const operlogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const defaultSort = ref({ prop: "operTime", order: "descending" });

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      operIp: undefined,
      title: undefined,
      operName: undefined,
      businessType: undefined,
      status: undefined
   }
});

const { queryParams, form } = toRefs(data);

const columns = ref([
   { key: 0, title: '日志编号', dataIndex: 'operId' },
   { key: 1, title: '系统模块', dataIndex: 'title' },
   { key: 2, title: '操作类型', dataIndex: 'businessType' },
   { key: 3, title: '操作人员', dataIndex: 'operName' },
   { key: 4, title: '操作地址', dataIndex: 'operIp' },
   { key: 5, title: '操作状态', dataIndex: 'status' },
   { key: 6, title: '操作日期', dataIndex: 'operTime' },
   { key: 7, title: '消耗时间', dataIndex: 'costTime' },
   { key: 8, title: '操作', dataIndex: 'operId' }
])
/** 查询登录日志 */
function getList() {
   loading.value = true;
   list(proxy.addDateRange(queryParams.value, dateRange.value)).then(({ data }) => {
      operlogList.value = data.rows;
      total.value = data.total;
      loading.value = false;
   });
}
/** 操作日志类型字典翻译 */
function typeFormat(row, column) {
   return proxy.selectDictLabel(sys_oper_type.value, row.businessType);
}
/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   queryParams.value.pageNum = 1;
   proxy.$refs["operlogRef"].sort(defaultSort.value.prop, defaultSort.value.order);
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.operId);
   multiple.value = !selection.length;
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
   queryParams.value.orderByColumn = column.prop;
   queryParams.value.isAsc = column.order;
   getList();
}
/** 详细按钮操作 */
function handleView(row) {
   open.value = true;
   form.value = row;
}
/** 删除按钮操作 */
function handleDelete(row) {
   const operIds = row.operId || ids.value;
   proxy.$modal.confirm('是否确认删除日志编号为"' + operIds + '"的数据项?').then(function () {
      return delOperlog(operIds);
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}
/** 清空按钮操作 */
function handleClean() {
   proxy.$modal.confirm("是否确认清空所有操作日志数据项?").then(function () {
      return cleanOperlog();
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
   }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
   proxy.download("monitor/operlog/export", {
      ...queryParams.value,
   }, `config_${new Date().getTime()}.xlsx`);
}

getList();
</script>
